package com.aibili.service;

import java.util.ArrayList;
import java.util.List;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.aibili.pojo.ChargeCard;
import com.aibili.pojo.ChargeRecord;
import com.aibili.pojo.GiftInfo;
import com.aibili.pojo.GiftRecord;
import com.aibili.pojo.UserAccount;
import com.aibili.pojo.UserInfo;
import com.aibili.pojo.model.PageBodyModel;
import com.aibili.pojo.model.PageModel;
import com.daoshun.common.CommonUtils;
import com.daoshun.common.DsConstant;

@Service("giftService")
@Transactional(readOnly = true, propagation = Propagation.SUPPORTS)
public class GiftService extends BaseService {

	/**
	 * 添加送礼物记录
	 * 
	 * @param record
	 */
	@Transactional(readOnly = false, propagation = Propagation.REQUIRED)
	public void SaveGiftRecord(GiftRecord record) {
		dataDao.addObject(record);
	}

	/**
	 * 我送出的礼物列表
	 * 
	 * @param parseLong
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public List<GiftRecord> getMySendGiftList(long from_id, int page, int pageSize) {
		String hql = " from GiftRecord where from_id = :from_id order by receive_time desc";
		List<GiftRecord> list = (List<GiftRecord>) dataDao.pageQueryViaParam(hql, pageSize, page,
				new String[] { "from_id" }, from_id);
		if (list != null && list.size() > 0) {
			for (GiftRecord giftRecord : list) {
				if (giftRecord.getGift_id() != 0) {
					GiftInfo info = dataDao.getObjectById(GiftInfo.class, giftRecord.getGift_id());
					if (info != null) {
						giftRecord.setGift(info);
					}
				}

				if (!CommonUtils.isEmptyString(giftRecord.getAnonymous_id())) {
					giftRecord.setTo_user_name("匿名用户");
				} else {
					UserInfo user = dataDao.getObjectById(UserInfo.class, giftRecord.getTo_id());
					if (user != null && user.getUser_name() != null) {
						giftRecord.setTo_user_name(user.getUser_name());
					} else {
						giftRecord.setTo_user_name(user.getMobile());
					}
				}

			}
		}
		return list;
	}

	/**
	 * 我收到的礼物列表
	 * 
	 * @param parseLong
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public List<GiftRecord> getMyGetGiftList(long to_id, int page, int pageSize) {
		String hql = " from GiftRecord where to_id = :to_id order by is_change, receive_time desc";
		List<GiftRecord> list = (List<GiftRecord>) dataDao.pageQueryViaParam(hql, pageSize, page,
				new String[] { "to_id" }, to_id);
		if (list != null && list.size() > 0) {
			for (GiftRecord giftRecord : list) {
				if (giftRecord.getGift_id() != 0) {
					GiftInfo info = dataDao.getObjectById(GiftInfo.class, giftRecord.getGift_id());
					if (info != null) {
						giftRecord.setGift(info);
					}
				}
			}
		}
		return list;
	}

	/**
	 * 新增绑定支付信息
	 * 
	 * @param account
	 */
	@Transactional(readOnly = false, propagation = Propagation.REQUIRED)
	public void SaveUserAccount(UserAccount account) {
		dataDao.addObject(account);
	}

	/**
	 * 添加充值记录
	 * 
	 * @param record
	 */
	@Transactional(readOnly = false, propagation = Propagation.REQUIRED)
	public void SaveChargeRecord(ChargeRecord record) {
		dataDao.addObject(record);
	}

	/**
	 * 
	 * @Description 通过 id 获取礼物信息
	 * @param id
	 * @return
	 */
	public GiftInfo getGiftInfo(long id) {
		GiftInfo info = dataDao.getObjectById(GiftInfo.class, id);
		return info;
	}

	/**
	 * 送出的总份数
	 * 
	 * @param from_id
	 * @return
	 */
	public int getMySendGiftCount(long from_id) {
		String hql = "select count(id) from GiftRecord where from_id = :from_id order by receive_time desc";
		long count = (long) dataDao.getFirstObjectViaParam(hql, new String[] { "from_id" }, from_id);
		return (int) count;
	}

	/**
	 * 送出的送爱币数
	 * 
	 * @param from_id
	 * @return
	 */
	public Long getMySendGiftPoint(long from_id) {
		String hql = "select sum(point) from GiftRecord where from_id = :from_id order by receive_time desc";
		Long count = (Long) dataDao.getFirstObjectViaParam(hql, new String[] { "from_id" }, from_id);
		if (count == null) {
			count = 0l;
		}
		return count;
	}

	/**
	 * 消费记录
	 * 
	 * @param user_id
	 * @param page
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public List<ChargeRecord> getChargeRecordByUser(long user_id, int page) {
		String hql = " from ChargeRecord where user_id = :user_id order by charge_time desc";
		List<ChargeRecord> list = (List<ChargeRecord>) dataDao.pageQueryViaParam(hql, DsConstant.PAGESIZE, page,
				new String[] { "user_id" }, user_id);
		return list;
	}

	/**
	 * 
	 * @Description 获取礼物信息
	 * @param pageIndex
	 * @param pageSize
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public PageBodyModel<GiftInfo> getByParams(Integer pageIndex, Integer pageSize) {
		String orderhql = " order by id desc ";
		String chql = " select count(g) from GiftInfo g ";
		String hql = " from GiftInfo " + orderhql;

		PageBodyModel<GiftInfo> result = new PageBodyModel<GiftInfo>();
		long count = (long) dataDao.getFirstObjectViaParam(chql, null);
		if (count > 0) {
			PageModel page = PageModel.createPage(count, pageIndex, pageSize);
			List<GiftInfo> banners = new ArrayList<GiftInfo>();
			banners = (List<GiftInfo>) dataDao.pageQueryViaParam(hql, pageSize, page.getPageIndex(), null);
			if (banners != null && banners.size() > 0) {
				for (GiftInfo giftInfo : banners) {
					if (!CommonUtils.isEmptyString(giftInfo.getPic())) {
						giftInfo.setPic(CommonUtils.getFileRootUrl() + giftInfo.getPic());
					}
				}
			}
			result = new PageBodyModel<>(page, banners);
		}
		return result;

	}

	/**
	 * 
	 * @Description 更新礼物信息
	 * @param info
	 */
	@Transactional(readOnly = false, propagation = Propagation.REQUIRED)
	public void updateGiftInfo(GiftInfo info) {
		dataDao.updateObject(info);
		;
	}

	/**
	 * 
	 * @Description 新增礼物信息
	 * @param info
	 */
	@Transactional(readOnly = false, propagation = Propagation.REQUIRED)
	public void addGiftInfo(GiftInfo info) {
		dataDao.addObject(info);
	}

	/**
	 * 
	 * @Description 通过 type 查找礼物
	 * @param type
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public List<GiftInfo> getGiftList(int type) {
		String hql = "from GiftInfo where 1=1";
		if (type == 0) {
			hql += " and time = 0";
		} else {
			hql += " and time > 0";
		}
		List<GiftInfo> list = (List<GiftInfo>) dataDao.getObjectsViaParam(hql, null);
		return list;
	}

	/**
	 * 
	 * @Description 收到的礼物的礼物头像
	 * @param user_id
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public List<String> getIndexGift(long user_id) {
		String hql = "select pic from GiftInfo where id in (select gift_id from GiftRecord where to_id = :user_id)";
		List<String> infos = (List<String>) dataDao.pageQueryViaParam(hql, 3, 1, new String[] { "user_id" }, user_id);
		return infos;
	}

	/**
	 * 
	 * @Description 情感中心的三个头像
	 * @param user_id
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public List<String> getIndexSend(long user_id) {
		// 查找三个不同的人的头像
		String hql = "select to_id from GiftRecord WHERE from_id = :user_id GROUP BY to_id ORDER BY receive_time desc";
		List<Long> infos = (List<Long>) dataDao.pageQueryViaParam(hql, 3, 1, new String[] { "user_id" }, user_id);
		String hql1 = "select portrait from UserInfo where id = :id";
		String[] paramstrs = new String[] { "id" };
		List<String> porlist = new ArrayList<>();
		if (infos != null && infos.size() > 0) {
			String portrait = "";
			for (Long long1 : infos) {
				portrait = (String) dataDao.getFirstObjectViaParam(hql1, paramstrs, long1);
				if (CommonUtils.isEmptyString(portrait)) {
					porlist.add("");
				} else {
					porlist.add(portrait);
				}

			}
		}
		return porlist;
	}

	/**
	 * 
	 * @Description 通过礼物记录id 查找礼物记录
	 * @param parseLong
	 * @return
	 */
	public GiftRecord getGiftRecordById(long id) {
		GiftRecord record = dataDao.getObjectById(GiftRecord.class, id);
		return record;
	}

	/**
	 * 
	 * @Description 修改送礼记录
	 * @param record
	 */
	@Transactional(readOnly = false, propagation = Propagation.REQUIRED)
	public void updateGiftRecord(GiftRecord record) {
		dataDao.updateObject(record);
	}

	/**
	 * 
	 * @Description 获取所有未删除的充值卡
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public List<ChargeCard> getChargeCard() {
		String hql = " from ChargeCard where is_delete = 0 ";
		List<ChargeCard> cards = (List<ChargeCard>) dataDao.pageQueryViaParam(hql, 6, 1, null);
		return cards;
	}

	/**
	 * 
	 * @Description 通过 id 获得充值卡
	 * @param id
	 * @return
	 */
	public ChargeCard getChargeCardById(long id) {
		ChargeCard card = dataDao.getObjectById(ChargeCard.class, id);
		return card;
	}

	/**
	 * 
	 * @Description 修改充值卡信息
	 * @param card
	 */
	@Transactional(readOnly = false, propagation = Propagation.REQUIRED)
	public void updateChargeCard(ChargeCard card) {
		dataDao.updateObject(card);
	}

	/**
	 * 
	 * @Description 新增充值卡信息
	 * @param card
	 */
	@Transactional(readOnly = false, propagation = Propagation.REQUIRED)
	public void addChargeCard(ChargeCard card) {
		dataDao.addObject(card);
	}

	@Transactional(readOnly = false, propagation = Propagation.REQUIRED)
	public void chageflg(String out_trade_no) {
		String hql = "from ChargeRecord where alipayordernum = :alipayordernum";
		ChargeRecord record = (ChargeRecord)dataDao.getFirstObjectViaParam(hql, new String[] { "alipayordernum"},out_trade_no);
		if(record!=null && record.getFlg()==0){
			UserInfo user = dataDao.getObjectById(UserInfo.class,record.getUser_id());
			user.setPoint(user.getPoint()+record.getCharge_num());
			record.setFlg(1);
			dataDao.updateObject(record);
			dataDao.updateObject(user);
		}
		
	}

	@Transactional(readOnly = false, propagation = Propagation.REQUIRED)
	public void changep(String batch_no) {
		String hql = "from ChargeRecord where alipayordernum = :alipayordernum";
		ChargeRecord record = (ChargeRecord)dataDao.getFirstObjectViaParam(hql, new String[] { "alipayordernum"},batch_no);
		if(record!=null && record.getFlg()==0){
			//UserInfo user = dataDao.getObjectById(UserInfo.class,record.getUser_id());
			//user.setPoint(user.getPoint()+record.getCharge_num());
			record.setProportion(2);
			dataDao.updateObject(record);
			//dataDao.updateObject(user);
		}
		
	}

}
